{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第 1 节　使用 Python 进行描述统计：单变量\n",
    "## 第 3 章　使用 Python 进行数据分析｜用 Python 动手学统计学"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1. 统计分析与 scipy"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "'%.3f'"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用于数值计算的库\n",
    "import numpy as np\n",
    "\n",
    "# 设置浮点数打印精度\n",
    "%precision 3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:07:15.182187Z",
     "end_time": "2024-04-15T20:07:15.368479Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2. 单变量数据的操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "array([2, 3, 3, 4, 4, 4, 4, 5, 5, 6])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fish_data = np.array([2, 3, 3, 4, 4, 4, 4, 5, 5, 6])\n",
    "fish_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:07:15.368479Z",
     "end_time": "2024-04-15T20:07:15.405424Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3. 实现：总和与样本容量"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "40"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 总和\n",
    "np.sum(fish_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:08:36.160608Z",
     "end_time": "2024-04-15T20:08:36.170114Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "40"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 参考\n",
    "fish_data.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:08:46.695870Z",
     "end_time": "2024-04-15T20:08:46.776324Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "40"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 参考\n",
    "sum(fish_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:08:57.309203Z",
     "end_time": "2024-04-15T20:08:57.414880Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "10"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本容量\n",
    "len(fish_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:09:06.075323Z",
     "end_time": "2024-04-15T20:09:06.128201Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4. 实现：均值（期望值）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "4.000"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算均值\n",
    "N = len(fish_data)\n",
    "sum_value = np.sum(fish_data)\n",
    "mu = sum_value / N\n",
    "mu"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:10:00.083516Z",
     "end_time": "2024-04-15T20:10:00.122058Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "4.000"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算均值的函数\n",
    "np.mean(fish_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:10:23.876462Z",
     "end_time": "2024-04-15T20:10:23.906878Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5. 实现：样本方差"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "1.200"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本方差\n",
    "sigma_2_sample = np.sum((fish_data - mu) ** 2) / N\n",
    "sigma_2_sample"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:11:25.535843Z",
     "end_time": "2024-04-15T20:11:25.631670Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "array([2, 3, 3, 4, 4, 4, 4, 5, 5, 6])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fish_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:11:50.268518Z",
     "end_time": "2024-04-15T20:11:50.294706Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-2., -1., -1.,  0.,  0.,  0.,  0.,  1.,  1.,  2.])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fish_data - mu"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:11:59.635608Z",
     "end_time": "2024-04-15T20:11:59.696913Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "array([4., 1., 1., 0., 0., 0., 0., 1., 1., 4.])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(fish_data - mu) ** 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:12:16.491481Z",
     "end_time": "2024-04-15T20:12:16.521463Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "12.000"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((fish_data - mu) ** 2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:12:35.872407Z",
     "end_time": "2024-04-15T20:12:35.945199Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "1.200"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算样本方差的函数\n",
    "np.var(fish_data, ddof=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:13:07.024682Z",
     "end_time": "2024-04-15T20:13:07.055614Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 6. 实现：无偏方差"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "1.333"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 无偏方差\n",
    "sigma_2 = np.sum((fish_data - mu) ** 2) / (N - 1)\n",
    "sigma_2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:23:09.946123Z",
     "end_time": "2024-04-15T20:23:10.013972Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "1.333"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 无偏方差\n",
    "np.var(fish_data, ddof=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:23:37.286620Z",
     "end_time": "2024-04-15T20:23:37.420645Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 7. 实现：标准差"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "1.155"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准差\n",
    "sigma = np.sqrt(sigma_2)\n",
    "sigma"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:29:31.436368Z",
     "end_time": "2024-04-15T20:29:31.525176Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "1.155"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算标准差的函数\n",
    "np.std(fish_data, ddof=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:29:49.781528Z",
     "end_time": "2024-04-15T20:29:49.849848Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8. 补充：标准化"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-2., -1., -1.,  0.,  0.,  0.,  0.,  1.,  1.,  2.])"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fish_data - mu"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:30:29.597278Z",
     "end_time": "2024-04-15T20:30:29.634538Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "0.000"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(fish_data - mu)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:30:50.334187Z",
     "end_time": "2024-04-15T20:30:50.410440Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1.732, 2.598, 2.598, 3.464, 3.464, 3.464, 3.464, 4.33 , 4.33 ,\n       5.196])"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fish_data / sigma"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:31:07.719779Z",
     "end_time": "2024-04-15T20:31:07.763463Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "1.000"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(fish_data / sigma, ddof=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:31:30.199509Z",
     "end_time": "2024-04-15T20:31:30.272854Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-1.732, -0.866, -0.866,  0.   ,  0.   ,  0.   ,  0.   ,  0.866,\n        0.866,  1.732])"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standard = (fish_data - mu) / sigma\n",
    "standard"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:31:51.216181Z",
     "end_time": "2024-04-15T20:31:51.266180Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "0.000"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(standard)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:32:08.519915Z",
     "end_time": "2024-04-15T20:32:08.553542Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "1.000"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(standard, ddof=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:32:25.307339Z",
     "end_time": "2024-04-15T20:32:25.394169Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 9. 补充：其他统计量"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "6"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最大值\n",
    "np.amax(fish_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:32:58.044316Z",
     "end_time": "2024-04-15T20:32:58.099184Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最小值\n",
    "np.amin(fish_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:33:13.088885Z",
     "end_time": "2024-04-15T20:33:13.162128Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "4.000"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 中位数\n",
    "np.median(fish_data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:33:33.697892Z",
     "end_time": "2024-04-15T20:33:33.763272Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [],
   "source": [
    "fish_data_2 = np.array([2, 3, 3, 4, 4, 4, 4, 5, 5, 100])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:33:44.937953Z",
     "end_time": "2024-04-15T20:33:44.946681Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "13.400"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(fish_data_2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:34:02.045101Z",
     "end_time": "2024-04-15T20:34:02.119661Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "4.000"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(fish_data_2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:34:17.199677Z",
     "end_time": "2024-04-15T20:34:17.307646Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 10. 实现：scipy.stats 与四分位数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "3.000"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy import stats\n",
    "\n",
    "fish_data_3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "stats.scoreatpercentile(fish_data_3, 25)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:34:55.223543Z",
     "end_time": "2024-04-15T20:34:57.556673Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "7.000"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.scoreatpercentile(fish_data_3, 75)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-15T20:35:20.326222Z",
     "end_time": "2024-04-15T20:35:20.340885Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
